<template>
  <div class="container">
    <!-- nav start -->
    <div class="service-list">
      <h1>{{$t('m.printservice.servicelist')}}</h1>
      <ul v-if="serviceList.length">
        <el-radio-group v-model="radio">
          <li v-for="(item, index) in serviceList" :key="index">
            <el-radio :label="index" :disabled="isAdd||isEdit">{{item.printservicename}}</el-radio>
            <i class="el-icon-remove-outline" @click="removeService(item)" v-if="hasPermission('delete')"></i>
          </li>
        </el-radio-group>
      </ul>
      <p class="tips" v-else>{{$t('m.printservice.noneservice')}}</p>
      <div class="btn-wrap">
        <el-button size="small" type="primary" @click="addService" v-if="hasPermission('add')">{{$t('m.printservice.addservice')}}</el-button>
      </div>
    </div>
    <!-- nav end -->

    <!-- service list detail start -->
    <div class="service-detail service-wrap" v-show="!isAdd&&serviceList.length">
      <h1>{{$t('m.printservice.serviceinfo')}}</h1>
      <el-row>
        <el-col :span="20">
          <el-form ref="form" :model="form" label-width="100px">
            <el-form-item :label="$t('m.printservice.servicename')">
              <template v-if="!isEdit">{{currentService.printservicename}}</template>
              <template v-else>
                <el-input size="small" v-model="form.serviceName" maxlength="100"></el-input>
              </template>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="4" class="icon-col">
          <template v-if="!isEdit">
            <i class="el-icon-setting" @click="editService" v-if="hasPermission('edit')"></i>
          </template>
          <template v-else>
            <el-button size="mini" type="primary" @click="saveEdit">{{$t('m.save')}}</el-button>
            <el-button size="mini" @click="isEdit=false;">{{$t('m.printservice.reset')}}</el-button>
          </template>
        </el-col>
      </el-row>
    </div>
    <!-- service list detail end -->

    <!-- service list add start -->
    <div class="service-add service-wrap" v-show="isAdd">
      <h1>{{$t('m.printservice.addservice')}}</h1>
      <el-row>
        <el-col :span="24">
          <el-form ref="form" :model="form" label-width="100px">
            <el-form-item :label="$t('m.printservice.servicename')">
              <el-input size="small" v-model="form.serviceName" maxlength="100"></el-input>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="24" align="center">
          <el-button size="mini" type="primary" @click="saveAdd">{{$t('m.save')}}</el-button>
          <el-button size="mini" @click="isAdd=false;">{{$t('m.printservice.back')}}</el-button>
        </el-col>
      </el-row>
    </div>
    <!-- service list add start -->
  </div>
</template>
<script>
export default {
  name:'printService',
  data(){
    return {
      serviceList:[],
      radio:0,
      isEdit:false,
      isAdd:false,
      form:{
        serviceName:''
      }
    }
  },
  mounted(){
    this.getData()
  },
  computed:{
    currentService(){
      return this.serviceList[this.radio] || {}
    }
  },
  methods:{
    //获取当前已添加数据的打印服务列表
    getData(){
      let options = {
        method:'get',
        url:`printservice/page`
      }
      this.$axios(options).then(({ data }) => {
        this.serviceList = data.records
      })
    },
    // 删除服务
    removeService(item){
      this.$confirm(
        `【${item.printservicename}】`+this.$t('m.deleteAlert'),
        this.$t('m.noticemessage'),
        {
          confirmButtonText:this.$t('m.confirm'),
          cancelButtonText:this.$t('m.cancel'),
          type:'warning'
        }
      ).then(() => {
        let options = {
          method:'delete',
          url:`printservice/delete/${item.printserviceid}`
        }
        this.$axios(options).then(({ message }) => {
          this.callbackMessage(message,'success')
          this.getData()
          this.radio = 0
        })
      }).catch(() => {
        this.callbackMessage(this.$t('m.printservice.cancel'), 'info')
      })
    },
    // 编辑服务
    editService(){
      this.isEdit = true
      this.form.serviceName = this.currentService.printservicename
    },
    // 保存编辑服务内容
    saveEdit(){
      if (!this.form.serviceName)
        return this.callbackMessage(this.$t('m.enter')+this.$t('m.printservice.servicename'),'success')
      let { printserviceid } = this.currentService
      let options = {
        method:'put',
        url:`printservice/update`,
        data:{
          printserviceid,
          printservicename:this.form.serviceName
        }
      }
      this.$axios(options).then(({ message }) => {
        this.callbackMessage(message,'success')
        this.getData()
        this.isEdit = false
      })
    },
    // 新增服务
    addService(){
      this.isAdd = true
    },
    // 保存新增服务内容
    saveAdd(){
      if (!this.form.serviceName)
        return this.callbackMessage(this.$t('m.enter')+this.$t('m.printservice.servicename'))
      let options = {
        method:'post',
        url:`printservice/add`,
        data:{ printservicename:this.form.serviceName }
      }
      this.$axios(options).then(({ message }) => {
        this.callbackMessage(message,'success')
        this.getData()
        this.isAdd = false
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.container {
  display: flex;
  h1 {
    font-size: 18px;
    line-height: 3;
  }
}
.service-list {
  width: 350px;
  padding-bottom: 20px;
  margin-right: 20px;
  & > * {
    background-color: #fff;
  }
  .el-radio-group {
    width: 100%;
  }
  ul {
    text-align: left;
    padding: 10px 20px;
    li {
      padding: 10px 0;
      display: flex;
      justify-content: space-between;
    }
  }
  .el-icon-remove-outline {
    font-size: 20px;
    color: #f56c6c;
    cursor: pointer;
  }
  .btn-wrap {
    padding: 20px 0;
  }
}
.service-wrap {
  text-align: left;
  flex: 1;
  & > * {
    background-color: #fff;
  }
  h1 {
    padding: 0 20px;
    border-bottom: 1px solid #c0c4cc;
  }
  .el-row {
    padding: 20px;
  }
  ::v-deep .el-form-item__content,
  ::v-deep .el-form-item__label {
    font-size: 16px;
  }
}
.service-detail {
  .icon-col {
    line-height: 40px;
    text-align: right;
  }
  .el-icon-setting {
    font-size: 20px;
    cursor: pointer;
    &:hover {
      color: #409eff;
    }
  }
}
</style>